﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Zzzxl.Util;

public class Map : MonoBehaviour {

    public static Map instance;
    public static GameObject chunkPrefab;
    public Dictionary<Vector3i, GameObject> chunks = new Dictionary<Vector3i, GameObject> ();

    private bool isSpawnChunks = false;

    void Awake () {
        instance = this;
        chunkPrefab = Resources.Load ("chunk") as GameObject;
    }

    public void CreateChunk(Vector3i pos) {
        if (isSpawnChunks) return; 
        StartCoroutine(SpawnChunk(pos));
    }

    private IEnumerator SpawnChunk (Vector3i position) {
        isSpawnChunks = true;
        Instantiate (chunkPrefab, position, Quaternion.identity);
        yield return null;
        isSpawnChunks = false;
    }

    public bool IsChunkExist(Vector3i pos) {
        return chunks.ContainsKey(new Vector3i(pos));
    }
}